package conf

import (
	xtime "fgo/advance/other/discovery/bilibili/example/pkg/time"
	"flag"
	"github.com/BurntSushi/toml"
	"github.com/bilibili/discovery/naming"
	"os"
	"strconv"
	"strings"
	"time"
)

var (
	confPath  string
	region    string
	zone      string
	deployEnv string
	host      string
	addrs     string
	weight    int64
	debug     bool

	// Conf conf
	Conf = &Config{}
)

func init() {
	var (
		defHost, _   = os.Hostname()
		defAddrs     = os.Getenv("ADDRS")
		defWeight, _ = strconv.ParseInt(os.Getenv("WEIGHT"), 10, 32)
		defDebug, _  = strconv.ParseBool(os.Getenv("DEBUG"))
	)
	flag.StringVar(&confPath, "conf", "example.toml", "default config path.")
	flag.StringVar(&region, "region", os.Getenv("REGION"), "avaliable region. or use REGION env variable, value: sh etc.")
	flag.StringVar(&zone, "zone", os.Getenv("ZONE"), "avaliable zone. or use ZONE env variable, value: sh001/sh002 etc.")
	flag.StringVar(&deployEnv, "deploy.env", os.Getenv("DEPLOY_ENV"), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
	flag.StringVar(&host, "host", defHost, "machine hostname. or use default machine hostname.")
	flag.Int64Var(&weight, "weight", defWeight, "load balancing weight, or use WEIGHT env variable, value: 10 etc.")
	flag.StringVar(&addrs, "addrs", defAddrs, "server public ip addrs. or use ADDRS env variable, value: 127.0.0.1 etc.")
	flag.BoolVar(&debug, "debug2", defDebug, "server debug. or use DEBUG env variable, value: true/false etc.")
}

func Init() (err error) {
	Conf = Default()
	_, err = toml.DecodeFile(confPath, &Conf)
	return
}

func Default() *Config {
	return &Config{
		Debug: debug,
		Env:   &Env{Region: region, Zone: zone, DeployEnv: deployEnv, Host: host, Weight: weight, Addrs: strings.Split(addrs, ",")},

		Discovery: &naming.Config{Region: region, Zone: zone, Env: deployEnv, Host: host},
		//Discovery: &naming.Config{
		//	Nodes: []string{"127.0.0.1:7171"}, // NOTE: 配置种子节点(1个或多个)，client内部可根据/discovery/nodes节点获取全部node(方便后面增减节点)
		//	Zone:  "sh1",
		//	Env:   "dev",
		//},
		HttpConfig: &httpConfig{
			Addr: ":8000",
		},
		RPCServer: &RPCServer{
			Network:           "tcp",
			Addr:              ":3109",
			Timeout:           xtime.Duration(time.Second),
			IdleTimeout:       xtime.Duration(time.Second * 60),
			MaxLifeTime:       xtime.Duration(time.Hour * 2),
			ForceCloseWait:    xtime.Duration(time.Second * 20),
			KeepAliveInterval: xtime.Duration(time.Second * 60),
			KeepAliveTimeout:  xtime.Duration(time.Second * 20),
		},
	}
}

type httpConfig struct {
	Addr string
}

type Config struct {
	Debug      bool
	Env        *Env
	Discovery  *naming.Config
	HttpConfig *httpConfig
	RPCServer  *RPCServer
}

// Env is env config.
type Env struct {
	Region    string
	Zone      string
	DeployEnv string
	Host      string
	Weight    int64
	Addrs     []string
}

// RPCServer is RPC server config.
type RPCServer struct {
	Network           string
	Addr              string
	Timeout           xtime.Duration
	IdleTimeout       xtime.Duration
	MaxLifeTime       xtime.Duration
	ForceCloseWait    xtime.Duration
	KeepAliveInterval xtime.Duration
	KeepAliveTimeout  xtime.Duration
}
